package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    //
    @GetMapping
    public Result list(String name, String label,@RequestParam (defaultValue = "1") Integer page, @RequestParam (defaultValue = "10") Integer pageSize)
    {
        log.info("查询参数：name={},label={},page={},pageSize={}",name,label,page,pageSize);
        PageResult<Role> pageResult = roleService.listRoles(name,label,page,pageSize);
        return Result.success(pageResult);
    }
    @Log
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id)
    {
        log.info("删除角色: {}" , id);
        roleService.deleleByid(id);
       return Result.success();
    }
    @Log
    @PostMapping
    public Result insert(@RequestBody Role role)
    {
        log.info("新增角色: {}" , role);
        roleService.insert(role);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id)
    {
        log.info("查询角色: {}" , id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Role role)
    {
        log.info("更新角色: {}",role);
        roleService.updaterole(role);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list()
    {
        log.info("查询所有角色");
       List<Role> rolelist =  roleService.selectAll();
        return Result.success(rolelist);
    }
}
